<?php namespace Manage\Http\Controllers;

use Manage\Http\Requests;
use Manage\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Manage\Device;
use Manage\Base;
use Manage\Agent;
use Input;
use View;
use Redirect;
use Session;

class AdminController extends Controller 
{

    /**
     * 首页
     * @return [type] [description]
     */
    protected function index()
    {
        //验证跳转
        if( empty($this->data['id']) )
            return View::make('login.index');
        
        //操作验证
        if( Session::has('notic') )
        {
            $notic = Session::get('notic');
            
            if( $notic == 1 )
            {
                Base::dPush('error', 0, $this);
                Base::dPush('msg', '操作成功!', $this);
            }
            else
            {
                Base::dPush('error', 1, $this);
                Base::dPush('msg', '操作失败!', $this);
            }
        }

    	//获取监控面板数据
    	$deviceList = Device::getAllDeviceData();

        Base::dPush('deviceList', $deviceList, $this);

        return View::make('dashboard', $this->data);
    }

    /**
     * 登录
     * @return
     */
    protected function login()
    {
        //校验登录
        if( true == Base::validate() )
        {

            if( false == Base::rememberGet() )
            {
                return View::make('login.index');
            }

            return View::make('login.index')->with(Base::rememberGet());
        }

        return View::make('login.index');
    }

    /**
     * 登录验证
     * @return [type] [description]
     */
    protected function loginCheck()
    {
        
        //验证
        $checkData = Agent::loginCheck(Input::only('username', 'password', 'remember'));

        list($id, $check, $msg) = $checkData;

        if( empty($id) )
        {
            return Redirect::to('login')->with($check, $msg);
        }
        return Redirect::to('/')->with($check, $msg);
    }

    /**
     * 用户登出
     * @return [type] [description]
     */
    protected function logout()
    {
        
        Agent::logout();

        return Redirect::to('login');
    }

    /**
     * 修改代理商信息
     * @param int 类型(1.信息修改; 2.密码修改)
     * @return [type] [description]
     */
    protected function edit($type)
    {
        //获取传入类型
        if( $type == 1 )
        {
            if( Input::has('edit') )
            {
                $data = Input::all();

                array_pull($data, 'edit');

                //信息修改
                $result = Agent::editAll(1, $data);

                if( !empty($result) )
                {
                    Session::put('nickname', $data['nickname']);
                    Session::put('province', $data['province']);
                    Session::put('city', $data['city']);
                }
            }
        }
        elseif( $type == 2 )
        {
            if( Input::has('edit') )
            {
                $data = Input::all();

                array_pull($data, 'edit');

                $result = Agent::editAll(2, $data);

            }
        }

        return Redirect::to('/')->with('notic',$result );
    }

    /**
     * 代理商管理
     * @return [type] [description]
     */
    protected function agents()
    {

        //创建代理商

        if( Input::has('username'))
        {
            $result = Agent::createAgents(Input::all());

            if(is_array($result))
            {
                Base::dPush('error', 1, $this);
                Base::dPush('msg', $result[1], $this);
            }
            else
            {
                Base::dPush('error', 0, $this);
                Base::dPush('msg', '操作成功!', $this);
            }
            return Redirect::to('agent');
        }

        //代理商列表
        $agentList = Agent::getAgents();

        Base::dPush('agentList', $agentList, $this);

        return View::make('agent.index', $this->data);
    }

}
